home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / man.3 / Hash.3 < prev    next >
Text File  |  1995-07-17  |  13KB  |  404 lines

  1. '\"
  2. '\" Copyright (c) 1989-1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/Hash.3,v 1.9 93/07/23 08:30:53 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .\" The definitions below are for supplemental macros used in Tcl/Tk
  24. .\" manual entries.
  25. .\"
  26. .\" .HS name section [date [version]]
  27. .\"    Replacement for .TH in other man pages.  See below for valid
  28. .\"    section names.
  29. .\"
  30. .\" .AP type name in/out [indent]
  31. .\"    Start paragraph describing an argument to a library procedure.
  32. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  33. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  34. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  35. .\"    needed;  use .AS below instead)
  36. .\"
  37. .\" .AS [type [name]]
  38. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  39. .\"    name are examples of largest possible arguments that will be passed
  40. .\"    to .AP later.  If args are omitted, default tab stops are used.
  41. .\"
  42. .\" .BS
  43. .\"    Start box enclosure.  From here until next .BE, everything will be
  44. .\"    enclosed in one large box.
  45. .\"
  46. .\" .BE
  47. .\"    End of box enclosure.
  48. .\"
  49. .\" .VS
  50. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  51. .\"    of man pages.
  52. .\"
  53. .\" .VE
  54. .\"    End of vertical sidebar.
  55. .\"
  56. .\" .DS
  57. .\"    Begin an indented unfilled display.
  58. .\"
  59. .\" .DE
  60. .\"    End of indented unfilled display.
  61. .\"
  62. '\"    # Heading for Tcl/Tk man pages
  63. .de HS
  64. .ds ^3 \\0
  65. .if !"\\$3"" .ds ^3 \\$3
  66. .if '\\$2'cmds'       .TH \\$1 1 \\*(^3 \\$4
  67. .if '\\$2'lib'        .TH \\$1 3 \\*(^3 \\$4
  68. .if '\\$2'tcl'        .TH \\$1 n \\*(^3 Tcl "Tcl Built-In Commands"
  69. .if '\\$2'tk'         .TH \\$1 n \\*(^3 Tk "Tk Commands"
  70. .if '\\$2'tclc'        .TH \\$1 3 \\*(^3 Tcl "Tcl Library Procedures"
  71. .if '\\$2'tkc'         .TH \\$1 3 \\*(^3 Tk "Tk Library Procedures"
  72. .if '\\$2'tclcmds'         .TH \\$1 1 \\*(^3 Tk "Tcl Applications"
  73. .if '\\$2'tkcmds'         .TH \\$1 1 \\*(^3 Tk "Tk Applications"
  74. .if t .wh -1.3i ^B
  75. .nr ^l \\n(.l
  76. .ad b
  77. ..
  78. '\"    # Start an argument description
  79. .de AP
  80. .ie !"\\$4"" .TP \\$4
  81. .el \{\
  82. .   ie !"\\$2"" .TP \\n()Cu
  83. .   el          .TP 15
  84. .\}
  85. .ie !"\\$3"" \{\
  86. .ta \\n()Au \\n()Bu
  87. \&\\$1    \\fI\\$2\\fP    (\\$3)
  88. .\".b
  89. .\}
  90. .el \{\
  91. .br
  92. .ie !"\\$2"" \{\
  93. \&\\$1    \\fI\\$2\\fP
  94. .\}
  95. .el \{\
  96. \&\\fI\\$1\\fP
  97. .\}
  98. .\}
  99. ..
  100. '\"    # define tabbing values for .AP
  101. .de AS
  102. .nr )A 10n
  103. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  104. .nr )B \\n()Au+15n
  105. .\"
  106. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  107. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  108. ..
  109. '\"    # BS - start boxed text
  110. '\"    # ^y = starting y location
  111. '\"    # ^b = 1
  112. .de BS
  113. .br
  114. .mk ^y
  115. .nr ^b 1u
  116. .if n .nf
  117. .if n .ti 0
  118. .if n \l'\\n(.lu\(ul'
  119. .if n .fi
  120. ..
  121. '\"    # BE - end boxed text (draw box now)
  122. .de BE
  123. .nf
  124. .ti 0
  125. .mk ^t
  126. .ie n \l'\\n(^lu\(ul'
  127. .el \{\
  128. .\"    Draw four-sided box normally, but don't draw top of
  129. .\"    box if the box started on an earlier page.
  130. .ie !\\n(^b-1 \{\
  131. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  132. .\}
  133. .el \}\
  134. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  135. .\}
  136. .\}
  137. .fi
  138. .br
  139. .nr ^b 0
  140. ..
  141. '\"    # VS - start vertical sidebar
  142. '\"    # ^Y = starting y location
  143. '\"    # ^v = 1 (for troff;  for nroff this doesn't matter)
  144. .de VS
  145. .mk ^Y
  146. .ie n 'mc \s12\(br\s0
  147. .el .nr ^v 1u
  148. ..
  149. '\"    # VE - end of vertical sidebar
  150. .de VE
  151. .ie n 'mc
  152. .el \{\
  153. .ev 2
  154. .nf
  155. .ti 0
  156. .mk ^t
  157. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  158. .sp -1
  159. .fi
  160. .ev
  161. .\}
  162. .nr ^v 0
  163. ..
  164. '\"    # Special macro to handle page bottom:  finish off current
  165. '\"    # box/sidebar if in box/sidebar mode, then invoked standard
  166. '\"    # page bottom macro.
  167. .de ^B
  168. .ev 2
  169. 'ti 0
  170. 'nf
  171. .mk ^t
  172. .if \\n(^b \{\
  173. .\"    Draw three-sided box if this is the box's first page,
  174. .\"    draw two sides but no top otherwise.
  175. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  176. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  177. .\}
  178. .if \\n(^v \{\
  179. .nr ^x \\n(^tu+1v-\\n(^Yu
  180. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  181. .\}
  182. .bp
  183. 'fi
  184. .ev
  185. .if \\n(^b \{\
  186. .mk ^y
  187. .nr ^b 2
  188. .\}
  189. .if \\n(^v \{\
  190. .mk ^Y
  191. .\}
  192. ..
  193. '\"    # DS - begin display
  194. .de DS
  195. .RS
  196. .nf
  197. .sp
  198. ..
  199. '\"    # DE - end display
  200. .de DE
  201. .fi
  202. .RE
  203. .sp .5
  204. ..
  205. .HS Tcl_Hash tclc
  206. .BS
  207. .SH NAME
  208. .na
  209. Tcl_InitHashTable, Tcl_DeleteHashTable, Tcl_CreateHashEntry, Tcl_DeleteHashEntry, Tcl_FindHashEntry, Tcl_GetHashValue, Tcl_SetHashValue, Tcl_GetHashKey, Tcl_FirstHashEntry, Tcl_NextHashEntry, Tcl_HashStats \- procedures to manage hash tables
  210. .SH SYNOPSIS
  211. .nf
  212. \fB#include <tcl.h>\fR
  213. .sp
  214. \fBTcl_InitHashTable\fR(\fItablePtr, keyType\fR)
  215. .sp
  216. \fBTcl_DeleteHashTable\fR(\fItablePtr\fR)
  217. .sp
  218. Tcl_HashEntry *
  219. \fBTcl_CreateHashEntry\fR(\fItablePtr, key, newPtr\fR)
  220. .sp
  221. \fBTcl_DeleteHashEntry\fR(\fIentryPtr\fR)
  222. .sp
  223. Tcl_HashEntry *
  224. \fBTcl_FindHashEntry\fR(\fItablePtr, key\fR)
  225. .sp
  226. ClientData
  227. \fBTcl_GetHashValue\fR(\fIentryPtr\fR)
  228. .sp
  229. \fBTcl_SetHashValue\fR(\fIentryPtr, value\fR)
  230. .sp
  231. char *
  232. \fBTcl_GetHashKey\fR(\fItablePtr, entryPtr\fR)
  233. .sp
  234. Tcl_HashEntry *
  235. \fBTcl_FirstHashEntry\fR(\fItablePtr, searchPtr\fR)
  236. .sp
  237. Tcl_HashEntry *
  238. \fBTcl_NextHashEntry\fR(\fIsearchPtr\fR)
  239. .sp
  240. char *
  241. \fBTcl_HashStats\fR(\fItablePtr\fR)
  242. .SH ARGUMENTS
  243. .AS Tcl_HashSearch *searchPtr
  244. .AP Tcl_HashTable *tablePtr in
  245. Address of hash table structure (for all procedures but
  246. \fBTcl_InitHashTable\fR, this must have been initialized by
  247. previous call to \fBTcl_InitHashTable\fR).
  248. .AP int keyType in
  249. Kind of keys to use for new hash table.  Must be either
  250. TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an integer value
  251. greater than 1.
  252. .AP char *key in
  253. Key to use for probe into table.  Exact form depends on
  254. \fIkeyType\fR used to create table.
  255. .AP int *newPtr out
  256. The word at \fI*newPtr\fR is set to 1 if a new entry was created
  257. and 0 if there was already an entry for \fIkey\fR.
  258. .AP Tcl_HashEntry *entryPtr in
  259. Pointer to hash table entry.
  260. .AP ClientData value in
  261. New value to assign to hash table entry.  Need not have type
  262. ClientData, but must fit in same space as ClientData.
  263. .AP Tcl_HashSearch *searchPtr in
  264. Pointer to record to use to keep track of progress in enumerating
  265. all the entries in a hash table.
  266. .BE
  267.  
  268. .SH DESCRIPTION
  269. .PP
  270. A hash table consists of zero or more entries, each consisting of
  271. a key and a value.
  272. Given the key for an entry, the hashing routines can very quickly
  273. locate the entry, and hence its value.
  274. There may be at most one entry in a hash table with a
  275. particular key, but many entries may have the same value.
  276. Keys can take one of three forms:  strings,
  277. one-word values, or integer arrays.
  278. All of the keys in a given table have the same form, which is
  279. specified when the table is initialized.
  280. .PP
  281. The value of a hash table entry can be anything that fits in
  282. the same space as a ``char *'' pointer.
  283. Values for hash table entries are managed entirely by clients,
  284. not by the hash module itself.
  285. Typically each entry's value is a pointer to a data structure
  286. managed by client code.
  287. .PP
  288. Hash tables grow gracefully as the number of entries increases,
  289. so that there are always less than three entries per hash bucket,
  290. on average.
  291. This allows for fast lookups regardless of the number of entries
  292. in a table.
  293. .PP
  294. \fBTcl_InitHashTable\fR initializes a structure that describes
  295. a new hash table.
  296. The space for the structure is provided by the caller, not by
  297. the hash module.
  298. The value of \fIkeyType\fR indicates what kinds of keys will
  299. be used for all entries in the table.  \fIKeyType\fR must have
  300. one of the following values:
  301. .IP \fBTCL_STRING_KEYS\fR 25
  302. Keys are null-terminated ASCII strings.
  303. They are passed to hashing routines using the address of the
  304. first character of the string.
  305. .IP \fBTCL_ONE_WORD_KEYS\fR 25
  306. Keys are single-word values;  they are passed to hashing routines
  307. and stored in hash table entries as ``char *'' values.
  308. The pointer value is the key;  it need not (and usually doesn't)
  309. actually point to a string.
  310. .IP \fIother\fR 25
  311. If \fIkeyType\fR is not TCL_STRING_KEYS or TCL_ONE_WORD_KEYS,
  312. then it must be an integer value greater than 1.
  313. In this case the keys will be arrays of ``int'' values, where
  314. \fIkeyType\fR gives the number of ints in each key.
  315. This allows structures to be used as keys.
  316. All keys must have the same size.
  317. Array keys are passed into hashing functions using the address
  318. of the first int in the array.
  319. .PP
  320. \fBTcl_DeleteHashTable\fR deletes all of the entries in a hash
  321. table and frees up the memory associated with the table's
  322. bucket array and entries.
  323. It does not free the actual table structure (pointed to
  324. by \fItablePtr\fR), since that memory is assumed to be managed
  325. by the client.
  326. \fBTcl_DeleteHashTable\fR also does not free or otherwise
  327. manipulate the values of the hash table entries.
  328. If the entry values point to dynamically-allocated memory, then
  329. it is the client's responsibility to free these structures
  330. before deleting the table.
  331. .PP
  332. \fBTcl_CreateHashEntry\fR locates the entry corresponding to a
  333. particular key, creating a new entry in the table if there
  334. wasn't already one with the given key.
  335. If an entry already existed with the given key then \fI*newPtr\fR
  336. is set to zero.
  337. If a new entry was created, then \fI*newPtr\fR is set to a non-zero
  338. value and the value of the new entry will be set to zero.
  339. The return value from \fBTcl_CreateHashEntry\fR is a pointer to
  340. the entry, which may be used to retrieve and modify the entry's
  341. value or to delete the entry from the table.
  342. .PP
  343. \fBTcl_DeleteHashEntry\fR will remove an existing entry from a
  344. table.
  345. The memory associated with the entry itself will be freed, but
  346. the client is responsible for any cleanup associated with the
  347. entry's value, such as freeing a structure that it points to.
  348. .PP
  349. \fBTcl_FindHashEntry\fR is similar to \fBTcl_CreateHashEntry\fR
  350. except that it doesn't create a new entry if the key doesn't exist;
  351. instead, it returns NULL as result.
  352. .PP
  353. \fBTcl_GetHashValue\fR and \fBTcl_SetHashValue\fR are used to
  354. read and write an entry's value, respectively.
  355. Values are stored and retrieved as type ``ClientData'', which is
  356. large enough to hold a pointer value.  On almost all machines this is
  357. large enough to hold an integer value too.
  358. .PP
  359. \fBTcl_GetHashKey\fR returns the key for a given hash table entry,
  360. either as a pointer to a string, a one-word (``char *'') key, or
  361. as a pointer to the first word of an array of integers, depending
  362. on the \fIkeyType\fR used to create a hash table.
  363. In all cases \fBTcl_GetHashKey\fR returns a result with type
  364. ``char *''.
  365. When the key is a string or array, the result of \fBTcl_GetHashKey\fR
  366. points to information in the table entry;  this information will
  367. remain valid until the entry is deleted or its table is deleted.
  368. .PP
  369. \fBTcl_FirstHashEntry\fR and \fBTcl_NextHashEntry\fR may be used
  370. to scan all of the entries in a hash table.
  371. A structure of type ``Tcl_HashSearch'', provided by the client,
  372. is used to keep track of progress through the table.
  373. \fBTcl_FirstHashEntry\fR initializes the search record and
  374. returns the first entry in the table (or NULL if the table is
  375. empty).
  376. Each susequent call to \fBTcl_NextHashEntry\fR returns the
  377. next entry in the table or
  378. NULL if the end of the table has been reached.
  379. A call to \fBTcl_FirstHashEntry\fR followed by calls to
  380. \fBTcl_NextHashEntry\fR will return each of the entries in
  381. the table exactly once, in an arbitrary order.
  382. It is unadvisable to modify the structure of the table, e.g.
  383. by creating or deleting entries, while the search is in
  384. progress.
  385. .PP
  386. \fBTcl_HashStats\fR returns a dynamically-allocated string with
  387. overall information about a hash table, such as the number of
  388. entries it contains, the number of buckets in its hash array,
  389. and the utilization of the buckets.
  390. It is the caller's responsibility to free the result string
  391. by passing it to \fBfree\fR.
  392. .PP
  393. The header file \fBtcl.h\fR defines the actual data structures
  394. used to implement hash tables.
  395. This is necessary so that clients can allocate Tcl_HashTable
  396. structures and so that macros can be used to read and write
  397. the values of entries.
  398. However, users of the hashing routines should never refer directly
  399. to any of the fields of any of the hash-related data structures;
  400. use the procedures and macros defined here.
  401.  
  402. .SH KEYWORDS
  403. hash table, key, lookup, search, value
  404.